
package com.netease.xone.widget.richtext;

import android.content.Context;
import android.text.Spannable;
import android.util.AttributeSet;

import com.netease.xone.widget.richtext.span.FaceSpanManager;
import com.netease.xone.widget.richtext.span.NickNameSpan;
import com.netease.xone.widget.richtext.span.SubjectSpan;
import com.netease.xone.widget.richtext.span.TopicSpan;

public class DiscussEditText extends BaseRichEditText {

    public DiscussEditText(Context context) {
        this(context, null);
    }

    public DiscussEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        // setMovementMethod(LinkMovementMethod.getInstance());
        addMatcher(NickNameSpan.REGULAR_NICKNAME);
        addMatcher(TopicSpan.REGULAR_TOPIC);
        addMatcher(SubjectSpan.REGULAR_SUBJECT);
        addMatcher(FaceSpanManager.REGULAR_FACE);
    }

    @Override
    public void onMatched(String regularExp, int start, int end) {
        Spannable sp = getText();
        if (regularExp.equals(NickNameSpan.REGULAR_NICKNAME)) {
            sp.setSpan(new NickNameSpan(), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        } else if (regularExp.equals(TopicSpan.REGULAR_TOPIC)) {
            sp.setSpan(new TopicSpan(), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        } else if (regularExp.equals(SubjectSpan.REGULAR_SUBJECT)) {
            sp.setSpan(new SubjectSpan(), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        } else if (regularExp.equals(FaceSpanManager.REGULAR_FACE)) {
            FaceSpanManager.parseFace(getContext(), sp, sp.subSequence(start, end).toString(),
                    start, end);
        }
    }

}
